﻿using System;
using System.Collections.Generic;
using System.Text;
using QLKCB.Common;
using QLKCB.Model;
using System.Data;

namespace QLKCB.Controller
{
    class cHoSoBenhAn
    {

        Utils utils = new Utils();
        ConnectDB connectDB = new ConnectDB();

        public string getMaBenhAn()
        {
            string prefixKey = "BA";
            DateTime today = DateTime.Today;
            String nowDate = today.ToString("yyyyMMdd");
            string sql_1 = "select top 1 MaBA from HoSoBenhAn where MaBA like @maphieu + '%' order by MaBA desc";
            object[] param_1 = { "@maphieu", prefixKey + nowDate };

            string maBA = utils.genKey(sql_1, param_1, 2, prefixKey);
            return maBA;
        }

        public DataTable getAllKhoa()
        {

            string sql = "select MaKhoa, TenKhoa from Khoa";
            object[] param = { };
            DataTable dtKhoa = connectDB.ExeQuery(sql, param);
            return dtKhoa;

        }

        public DataTable getBacSiByKhoa(string maKhoa)
        {

            string sql = "select MaBS, TenBS from BacSi where MaKhoa=@makhoa";
            object[] param = { "@makhoa", maKhoa };
            DataTable dtBacSi = connectDB.ExeQuery(sql, param);
            return dtBacSi;

        }

        public int insertBenhAn(string maBA, string maBS, string maBN, string maTHBN, DateTime ngayLap, DateTime thoiGianNhapVien, DateTime thoiGianXuatVien, string lyDo, string benhSu, string chuanDoan)
        {

            string sql = "insert into HoSoBenhAn values (@maba, @mabs, @mabn, @mathbn, @ngaylap, @tgnhap, @tgxuat, @lydo, @benhsu, @chuandoan)";
            object[] param = { "@maba", maBA, "@mabs", maBS, "@mabn", maBN, "@mathbn", maTHBN, "@ngaylap", ngayLap, "@tgnhap", thoiGianNhapVien, "@tgxuat", thoiGianXuatVien, "@lydo", lyDo, "@benhsu", benhSu, "@chuandoan", chuanDoan };
            int result = connectDB.ExeNonQuery(sql, param);
            return result;

        }

        public int updateBenhAn(string maBA, string maBS, string maBN, string maTHBN, DateTime ngayLap, DateTime thoiGianNhapVien, DateTime thoiGianXuatVien, string lyDo, string benhSu, string chuanDoan)
        {

            string sql = "update HoSoBenhAn set MaBS=@mabs, MaBN=@mabn, MaTHBN=@mathbn, NgayLapHSBA=@ngaylap, ThoiGianNhapVien=@tgnhap, ThoiGianXuatVien=@tgxuat, LyDoNhapVien=@lydo, BenhSu=@benhsu, ChanDoanLucRa=@chuandoan where MaBA=@maba";
            object[] param = { "@maba", maBA, "@mabs", maBS, "@mabn", maBN, "@mathbn", maTHBN, "@ngaylap", ngayLap, "@tgnhap", thoiGianNhapVien, "@tgxuat", thoiGianXuatVien, "@lydo", lyDo, "@benhsu", benhSu, "@chuandoan", chuanDoan };
            int result = connectDB.ExeNonQuery(sql, param);
            return result;

        }

        public string getMaBAExists(string maBN)
        {
            string sql = "select MaBA from HoSoBenhAn where MaBN=@mabn and MaTHBN='CHUAXET'";
            object[] param = { "@mabn", maBN };
            DataTable dtMaBA = connectDB.ExeQuery(sql, param);
            if (dtMaBA.Rows.Count > 0)
                return dtMaBA.Rows[0][0].ToString(); ;
            return "";
        }

        public DataTable getHSBAByMaBA(string maBA)
        {
            string sql = "select * from HoSoBenhAn where MaBA=@maba";
            object[] param = { "@maba", maBA };
            DataTable dtHSBA = connectDB.ExeQuery(sql, param);
            return dtHSBA;
        }

        public string getKhoaByBacSi(string maBS)
        {
            string sql = "select MaKhoa from BacSi where MaBS=@mabs";
            object[] param = { "@mabs", maBS };
            DataTable dtKhoa = connectDB.ExeQuery(sql, param);
            return dtKhoa.Rows[0][0].ToString();
        }

    }
}
